﻿using Omabar.BLL;
using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Omabar.CMS.CMS
{
    public partial class PhotoList : System.Web.UI.Page
    {
        protected List<PHOTO> photos;
        protected int pageCount;
        protected int pageNo = 1;
        protected int groupNo = 1;
        protected int groupCount = 1;
        protected string keywords;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pageNo = string.IsNullOrEmpty(Request["pageno"]) ? 1 : int.Parse(Request["pageno"]);

                Photo_BLL bll = new Photo_BLL();
                keywords = Request["keywords"];
                string sqllist = "select Top 10 PHOTO_ID,PHOTO_NAME,PHOTO_URL,PHOTO_DES,PHOTO_HOT,PHOTO_COVER from (select row_number() over(order by PHOTO_ID desc) as rownumber,* from PHOTO {0}) A where rownumber > {1}";
                string sqlcount = "select count(*) from PHOTO";
                if (!string.IsNullOrEmpty(keywords))
                {
                    string filter = string.Format(" where PHOTO_NAME like '%{0}%'", keywords);
                    sqlcount += filter;
                    sqllist = string.Format(sqllist, filter, (pageNo - 1) * 10);
                }

                sqllist = string.Format(sqllist, keywords, (pageNo - 1) * 10);
                photos = bll.GetModels(sqllist);
                if (photos == null) photos = new List<PHOTO>();

                #region 计算分页
                int total = bll.GetCount(sqlcount);
                pageCount = total % 10 == 0 ? total / 10 : total / 10 + 1;
                groupNo = pageNo % 10 == 0 ? pageNo / 10 : pageNo / 10 + 1;
                groupCount = pageCount % 10 == 0 ? pageCount / 10 : pageCount / 10 + 1;
                #endregion
            }
        }
    }
}